package cn.com.sbell.vss.auth;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;

import cn.com.sbell.vss.ILogCommander;
import cn.com.sbell.vss.IProtectedCommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.VissSession;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.util.SessionHolder;

/**
 * @author Yifu.HUANG
 * 
 */
public class UserLogout implements IProtectedCommander, ILogCommander {

	private SessionHolder sessionHolder;

	/**
	 * (non-Javadoc)
	 * 
	 * @see cn.com.sbell.vss.ICommander#execute(javax.servlet.http.HttpServletRequest)
	 */
	public Response execute(HttpServletRequest request) {
		// prepare response (include parameters)
		Response response = Response.createResponse(request);
		Document doc = DocumentHelper.createDocument();
		doc.addElement("parameters");

		// destory the session

		VissSession data = (VissSession) request
				.getAttribute(VssConstants.KEY_USER_SESSION_DATA);
		if (data != null) {
			sessionHolder.removeSession(data.getSessionId());
			request.removeAttribute(VssConstants.KEY_USER_SESSION_DATA);
		}

		// set parameters xml string, then return
		response.setParameters(doc.getRootElement());
		return response;
	}

	public void setSessionHolder(SessionHolder sessionHolder) {
		this.sessionHolder = sessionHolder;
	}

}
